package com.yyt_study.bdmap_postion_demo2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends Activity implements OnClickListener, BDLocationListener {
	private MapView mMapView = null;
	BaiduMap mBaiduMap = null;
	LocationClient mLocationClient = null;
	MyLocationData locData;
	private Button btnStart, btnStop;;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 在使用SDK各组件之前初始化context信息，传入ApplicationContext
		// 注意该方法要再setContentView方法之前实现
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.activity_main);
		initView();
		initMap();
		initLocationOption();
	}

	private void initView()
	{
		btnStart = (Button) findViewById(R.id.btn_start);
		btnStop = (Button) findViewById(R.id.btn_stop);
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);

	}

	private void initMap()
	{
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();

	}

	/**
	 * @Title: initLocationOption
	 * @Description: 设置定位的相关信息
	 * @param 设定文件
	 * @author username
	 * @return void 返回类型
	 * @throws
	 */

	private void initLocationOption()
	{
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mLocationClient.registerLocationListener(this); // 注册监听函数
		LocationClientOption locationOption = new LocationClientOption();
		locationOption.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式为高精度模式
		locationOption.setCoorType("bd09ll"); // 返回定位结果为百度经纬度，默认值为gcj02
		locationOption.setIsNeedAddress(true);
		mLocationClient.setLocOption(locationOption);

	}

	
	/**
	  * <p>实现定位功能: </p>
	  * @param location
	  * @see com.baidu.location.BDLocationListener#onReceiveLocation(com.baidu.location.BDLocation)
	  */
	
	@Override
	public void onReceiveLocation(BDLocation location)
	{
		Log.i("info", "-------------------llll");
		if (location != null) {
			locData = new MyLocationData.Builder().accuracy(location.getRadius()).direction(100)
					.latitude(location.getLatitude()).longitude(location.getLongitude()).build();

			LatLng cenpt = new LatLng(location.getLatitude(), location.getLongitude());		//获取经纬度
			MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(17).build();		 //地图状态构造器
			MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //描述地图状态将要发生的变化
			mBaiduMap.setMyLocationEnabled(true);		// 开启定位图层  
			mBaiduMap.setMyLocationData(locData);		// 设置定位数据  
			mBaiduMap.setMapStatus(mMapStatusUpdate);	//改变地图状态
		}

	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
		mMapView.onDestroy();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
		mMapView.onPause();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.btn_start:
			mLocationClient.start();

			break;
		case R.id.btn_stop:
			mLocationClient.stop();
		default:
			break;
		}
	}
}
